#include "IniInfo.h"
using namespace inilib;


IniInfo::IniInfo ()
{
}


IniInfo::~IniInfo ()
{
	auto iterSection = m_Sections.begin();
	while ( iterSection != m_Sections.end() )
	{
		auto iterOption = iterSection->second.m_Options.begin();
		while ( iterOption != iterSection->second.m_Options.end() )
		{
			if ( iterOption->second != nullptr )
			{
				delete iterOption->second;
				iterOption->second = nullptr;
			}
			++iterOption;
		}

		++iterSection;
	}
}


void IniInfo::AddSection ( const std::string & sectionName, Mandatory isMandatory )
{
	auto iter = m_Sections.find( sectionName );
	if ( iter != m_Sections.end() )
	{
		std::stringstream ss;
		ss << "AddSection: section \"" << sectionName << "\" already exists";
		throw ExceptionIniLib( ss.str() );
	}

	m_Sections.insert( std::make_pair( sectionName, SectionInfo( sectionName, isMandatory == MANDATORY ) ) );
}


void IniInfo::AddOption ( const std::string & sectionName, OptionType * option )
{
	auto iter = m_Sections.find( sectionName );
	if ( iter == m_Sections.end() )
	{
		std::stringstream ss;
		ss << "Section \"" << sectionName << "\" doesn't exist";
		delete option;
		throw ExceptionIniLib( ss.str() );
	}

	iter->second.m_Options.insert( std::make_pair( option->GetName(), option ) );
}